/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication13;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Irina
 */
public class Copy implements ICommandExecutor{
    public static ICommandExecutor instance;
    
    /**
     *
     * @param InputString
     */
    @Override
    public void execute(String InputString) 
    {
        String[] strOut=InputString.split(" ");
        if(strOut.length<3)
        {
            System.out.println("Некорректный формат команды.Повторите попытку.");
        }else
        {
            String NameSourceFile=strOut[1];;
            String NameDestFile=strOut[2];
           
            File source=new File(NameSourceFile);
            if(!source.exists()){System.out.println("Файл-источник по указанному адресу не существует.");}
            else{
                File f=new File(NameDestFile);
                if (!f.exists()){System.out.println("Расположение файла для копирования задано не корректно.");}
                else {
                    File dest=new File(NameDestFile+"\\"+source.getName());
                    copy(source,dest);
                    if(dest.exists()){System.out.println("Копирование успешно завершено!"); System.out.println();}
                    }
                }
            }
    }   
    
        private void copy(File source,File dest)
    {
        if(source.isDirectory())
        {
            dest.mkdir(); 
            String [] children=source.list();
            for(String elem:children) 
            {
                copy(new File(source,elem),new File(dest,elem));
            }
        } else { 
            try
            {
                FileChannel sourceChannel = new FileInputStream(source).getChannel();
                FileChannel destChannel = new FileOutputStream(dest).getChannel();
                destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
                sourceChannel.close();
                destChannel.close();
                if(!dest.exists())
                {
                    System.out.println("Операция копирования не удалась");
                }System.out.println();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Copy.class.getName()).log(Level.SEVERE, null, ex);
            }catch (IOException ex) {
                Logger.getLogger(Copy.class.getName()).log(Level.SEVERE, null, ex);
            }  
        }  
    }

    public static ICommandExecutor getInstance()
    {
         if(instance==null)
        {
            instance=new Copy();
        }
        return instance;
    }
    
}
